//
//  MyScrollView.m
//  MobilecloudOffice
//
//  Created by milo on 15-9-1.
//  Copyright (c) 2015年 youxh. All rights reserved.
//

#import "MyScrollView.h"
//#import "picprogressview.h"
@implementation MyScrollView


@synthesize image;


#pragma mark -
#pragma mark === Intilization ===
#pragma mark -
- (id)initWithFrame:(CGRect)frame
{
    if ((self = [super initWithFrame:frame]))
	{
		self.delegate = self;
		self.minimumZoomScale = 0.5;
		self.maximumZoomScale = 2.5;
		self.showsVerticalScrollIndicator = NO;
		self.showsHorizontalScrollIndicator = NO;
		
		imageView  = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
		imageView.contentMode = UIViewContentModeScaleAspectFit;
		[self addSubview:imageView];
        
        UITapGestureRecognizer *singleTapGestureRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(singleTap:)];
        
        [singleTapGestureRecognizer setNumberOfTapsRequired:1];
        
        [self addGestureRecognizer:singleTapGestureRecognizer];
        
        
        
        UITapGestureRecognizer *doubleTapGestureRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(doubleTap:)];
        
        [doubleTapGestureRecognizer setNumberOfTapsRequired:2];
        
        [self addGestureRecognizer:doubleTapGestureRecognizer];
        
        
        
        //这行很关键，意思是只有当没有检测到doubleTapGestureRecognizer 或者 检测doubleTapGestureRecognizer失败，singleTapGestureRecognizer才有效
        
        [singleTapGestureRecognizer requireGestureRecognizerToFail:doubleTapGestureRecognizer];
    }
    return self;
}






//- (void)setImage:(UIImage *)img
//{
//	imageView.image = img;
//}
-(void)setImagewithpath:(NSString *)path{
    UIImage*img=[UIImage imageWithContentsOfFile:path];
    imageView.image=img;
    if (img.size.width<=imageView.frame.size.width&&img.size.height<=imageView.frame.size.height) {
        imageView.frame=CGRectMake(( self.frame.size.width-img.size.width)/2, (self.frame.size.height-img.size.height)/2, img.size.width, img.size.height);
        
    }else{
        imageView.contentMode=UIViewContentModeScaleAspectFit;
        imageView.frame=CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
    }

}
-(void)setImagewithurl:(NSString *)url{
    imageView.frame=CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);

    [imageView setImageWithURL:[NSURL URLWithString:url] placeholderImage:nil options:SDWebImageRetryFailed progress:^(NSUInteger receivedSize, long long expectedSize) {
        self.userInteractionEnabled = NO;
        if (expectedSize!=-1) {
        }
        
    } completed:^(UIImage *image1, NSError *error, SDImageCacheType cacheType) {
        self.userInteractionEnabled=YES;
        if (error==nil) {
            if (image1.size.width<=imageView.frame.size.width&&image1.size.height<=imageView.frame.size.height) {
                imageView.frame=CGRectMake(( self.frame.size.width-image1.size.width)/2, (self.frame.size.height-image1.size.height)/2, image1.size.width, image1.size.height);
                
            }else{
                imageView.contentMode=UIViewContentModeScaleAspectFit;
                imageView.frame=CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
            }

        }else{

            imageView.image=[UIImage imageNamed:@"picshowerror.png"];
            imageView.frame=CGRectMake(( self.frame.size.width-200)/2, (self.frame.size.height-200)/2, 200, 200);

        }
    }];
}
#pragma mark -
#pragma mark === UIScrollView Delegate ===
#pragma mark -
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
	return imageView;
}

- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale
{
	//NSLog(@"%s", _cmd);
	
	CGFloat zs = scrollView.zoomScale;
	zs = MAX(zs, 1.0);
	zs = MIN(zs, 2.0);
	
	[UIView beginAnimations:nil context:NULL];
	[UIView setAnimationDuration:0.3];
	scrollView.zoomScale = zs;
	[UIView commitAnimations];
}

#pragma mark -
#pragma mark === UITouch Delegate ===
#pragma mark -
//- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
//{
//	//NSLog(@"%s", _cmd);
//	
//	UITouch *touch = [touches anyObject];
//	
//	if ([touch tapCount] == 2)
//	{
//		//NSLog(@"double click");
//		
//		CGFloat zs = self.zoomScale;
//		zs = (zs == 1.0) ? 2.0 : 1.0;
//		
//		[UIView beginAnimations:nil context:NULL];
//		[UIView setAnimationDuration:0.3];
//		self.zoomScale = zs;
//		[UIView commitAnimations];
//	}else{
//        NSLog(@"%@",@"1");
//
//    }
//}
- (void)singleTap:(UIGestureRecognizer*)gestureRecognizer

{
    
    NSLog(@"-----singleTap-----");
    NSNotification * notice = [NSNotification notificationWithName:@"singleTap" object:nil];
    [[NSNotificationCenter defaultCenter] postNotification:notice];
    
}


- (void)doubleTap:(UIGestureRecognizer*)gestureRecognizer

{
    
    CGFloat zs = self.zoomScale;
    zs = (zs == 1.0) ? 2.0 : 1.0;
    
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.3];
    self.zoomScale = zs;
    [UIView commitAnimations];
    
    
}
#pragma mark -
#pragma mark === dealloc ===
#pragma mark -



@end
